//
//  exshangpinzuViewController.m
//  BAOASystem
//
//  Created by michael on 2018/6/1.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "exshangpinzuViewController.h"
#import "LZPickerView.h"

@interface exshangpinzuViewController ()
@property(nonatomic,strong)LZPickerView *lzPickerVIew;

@end

@implementation exshangpinzuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"LZPickerView" owner:nil options:nil];
    self.lzPickerVIew  = views[0];
        self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
[self initview];
        [self loadData];

    // Do any additional setup after loading the view.
}
- (void)loadData{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><id>%@</id></body></message>",userName,password,website,language,BAtype,_idstring];
  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bgetProductList];
       [HttpTools requestWithURL:path arrays:array method:@"getGroup" success:^(id JSON) {
         NSLog(@"%@",JSON);
_dic = JSON;                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){

  if(![_dic[@"ProductGroup"]isEqual:[NSNull null]]){
    _textfield.text =_dic[@"ProductGroup"][@"name"];
        _specialZJ=_dic[@"ProductGroup"][@"contactsId"];
        NSArray *array = _dic[@"ctslist"];
  for (NSDictionary *dic in array) {
   
   if ([_specialZJ isEqualToString:dic[@"id"]]) {
   _specialZJ = dic[@"id"];
 _zjspecialName = dic[@"ctname"];
  [self.button2 setTitle:dic[@"ctname"] forState:UIControlStateNormal];

}
}
if([[NSString stringWithFormat:@"%@",_dic[@"ProductGroup"][@"isPrivate"]] isEqualToString:@"0"] ){
_gongkaiid = @"0";
_button1.selected = YES;
}
    }
    }
       } failure:^(NSError *error) {
//            [self.tableView headerEndRefreshing];

//        [self.tableView footerEndRefreshing];
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    }];
}
- (void)initview{
_view0 = [[UIView alloc]initWithFrame:CGRectMake(10, 5, KScreenWidth-20, 80)];
_view0.backgroundColor =[UIColor whiteColor];
[self.view addSubview:_view0];
_view1 = [[UIView alloc]initWithFrame:CGRectMake(5, 39, _view0.width-10, 1)];
_view1.backgroundColor =[UIColor colorWithHexString:@"f0eff5"];
[_view0 addSubview:_view1];
NSString *string = NSLocalizedString(@"组名", nil);
_label1 = [[UILabel alloc ]initWithFrame:CGRectZero];
_label1.text = string;
_label1.textColor = [UIColor colorWithHexString:@"484848"];
[_label1 setFont:[UIFont systemFontOfSize:13]];
    CGFloat width = [UILabel getWidthWithTitle:_label1.text font:_label1.font];
    _label1.frame = CGRectMake(10, 0, width, 39);
[_view0 addSubview:_label1];
      _textfield = [[zhucetextfield alloc]init];
     _textfield.delegate = self;
 _textfield.frame = CGRectMake(_label1.right+10,5, 150, 29);
[_view0 addSubview:_textfield];
_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button1 setImage:[UIImage imageNamed:@"单选-默认"] forState:UIControlStateNormal];
[_button1 setTitle:NSLocalizedString(@"私人商品组", nil) forState:UIControlStateNormal];
[_button1 setImage:[UIImage imageNamed:@"单选-选中"] forState:UIControlStateSelected];
    _button1.titleLabel.font = [UIFont systemFontOfSize: 13.0];

[_button1 addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];
_button1.width = _view0.width-20-_label1.width-_textfield.width-10;
_button1.frame = CGRectMake(_textfield.right+10, 0, _button1.width, 39);
    [_button1 setTitleColor:[UIColor colorWithHexString:@"484848"]forState:UIControlStateNormal];
        [_button1 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft
                                    imageTitleSpace:5];
         [_view0 addSubview:_button1];
    
_label2 = [[UILabel alloc ]initWithFrame:CGRectZero];
_label2.text = NSLocalizedString(@"分配联系人", nil);
_label2.textColor = [UIColor colorWithHexString:@"484848"];
[_label2 setFont:[UIFont systemFontOfSize:13]];
    CGFloat width2 = [UILabel getWidthWithTitle:_label2.text font:_label2.font];
    _label2.frame = CGRectMake(10, 40, width2, 40);
             [_view0 addSubview:_label2];
     _button2 = [zhucebutton buttonWithType:UIButtonTypeCustom];
      _button2.frame = CGRectMake(_label2.right+ 10, 45, 150, 30);
          [_button2 addTarget:self action:@selector(zjyoupaction) forControlEvents:UIControlEventTouchUpInside];

UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(_button2.width-30,5 , 20, 20)];
image.image = [UIImage imageNamed:@"下拉"];
[_button2 addSubview:image];
             [_view0 addSubview:_button2];

_button3 = [UIButton buttonWithType:UIButtonTypeCustom];
      _button3.frame = CGRectMake((KScreenWidth-100)/2, _view0.bottom+10, 100, 40);
          _button3.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];

    [_button3 setTitle:NSLocalizedString(@"提交", nil) forState:UIControlStateNormal];
    _button3.titleLabel.font = [UIFont systemFontOfSize: 14.0];
          [_button3 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
        _button3.layer.cornerRadius = _button3.height/5;
                [_button3 addTarget:self action:@selector(action3) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:_button3];

}
- (void)zjyoupaction{
NSArray *array = _dic[@"ctslist"];
NSMutableArray *muaarray = [NSMutableArray array];
for (NSDictionary *dic in array) {
    [muaarray addObject:dic[@"ctname"]];
}
[self.lzPickerVIew lzPickerVIewType:LZPickerViewTypeSexAndHeight];
self.lzPickerVIew.dataSource =muaarray;
    [self.lzPickerVIew show];
         __weak typeof(self) weakSelf = self;
          self.lzPickerVIew.selectValue  = ^(NSString *value){

                 [weakSelf.button2 setTitle:value forState:UIControlStateNormal];
      _zjspecialName = value;
     for (NSDictionary *dic in array) {
   
   if ([value isEqualToString:dic[@"ctname"]]) {
   _specialZJ = dic[@"id"];
}
}
    };
}
- (void)action3{
   NSString *string = [_textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  if ([string isEqualToString:@""]|| string.length == 0){
     [self.view makeToast:NSLocalizedString(@"组名为空", nil)];
}else if ([_zjspecialName isEqualToString:@""]|| _zjspecialName.length == 0){
     [self.view makeToast:NSLocalizedString(@"分配联系人为空", nil)];


}else{

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
    if(_idstring.length>0){
   }else{
      _idstring = @"";

   }
   NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><ProductGroup><id>%@</id><name>%@</name><isPrivate>%@</isPrivate><contactsId>%@</contactsId></ProductGroup></body></message>",userName,password,website,language,BAtype,_idstring,string,_gongkaiid,_specialZJ];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
      NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bgetProductList];
       [HttpTools requestWithURL:path arrays:array method:@"modifyGroup" success:^(id JSON) {
           NSLog(@"%@",JSON);
         [self.view makeToast:JSON[@"msg"]];
if([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"]){
[self performSelector: @selector(popaction) withObject: nil afterDelay:1.0f];

}
} failure:^(NSError *error) {

    }];

}
}
- (void)popaction{
//    [defaults removeObjectForKey:@"dic"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshData" object:nil]; //刷新发件箱，收件箱 数据

[self.navigationController popViewControllerAnimated:YES];

}
- (void)action1:(UIButton *)button{
button.selected = !button.selected;
if (button.selected) {
    _gongkaiid = @"0";
}else{
    _gongkaiid = @"1";

}
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
